aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/send/invoice/[id]/+page.server.ts
blob: f294f88d457ef9d7a68a891da85b20302900e5c8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { error, redirect } from "@sveltejs/kit";
import { fd, auth, get, post } from "$lib/utils";

export async function load({ params: { id }, parent }) {
  let { rates, user } = await parent();

  let invoice = await get(`/invoice/${id}`);

  if (invoice.prompt && invoice.tip === null)
    redirect(307, `/invoice/${id}/tip`);

  if (invoice.memoPrompt && invoice.memo === null)
    redirect(307, `/invoice/${id}/memo`);

  if (user && invoice.account === user?.id)
    error(500, { message: "Cannot send to self" });

  if (!user) redirect(307, `/invoice/${id}`);

  return { invoice, rates, user };
}

export const actions = {
  default: async ({ cookies, params: { id }, request }) => {
    let p;
    try {
      let body = await fd(request);
      body.hash = id;

      p = await post("/payments", body, auth(cookies));
    } catch (e: any) {
      console.log("payment failed", id, e);
      error(500, e.message);
    }

    redirect(307, `/sent/${p.id}`);
  },
};